package ${package_name}.service;

import com.chdz.common.dynamic.Criteria;
import com.chdz.common.dynamic.Restrictions;
import com.chdz.common.mysql.entity.${model_name};
import com.chdz.common.mysql.repository.${model_name}Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
import org.springframework.hateoas.Link;
import com.chdz.common.util.BeanUtils;
import com.chdz.common.util.LinkUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import com.chdz.common.model.SysRuntimeException;
import org.springframework.hateoas.PagedResources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.chdz.common.request.Add${model_name}Request;
import com.chdz.common.request.Update${model_name}Request;
import com.chdz.common.response.${model_name}Response;
import com.chdz.common.response.CreatedResponse;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import org.apache.commons.lang.StringUtils;


/**
* 描述：${table_annotation}服务层
* @author ${author}
* @date ${date}
*/
@Service
public class ${model_name}Service extends CommonService{

    @Autowired
    private ${model_name}Repository ${model_name?uncap_first}Repository;
   
    public ResponseEntity<?> get${model_name}s(String keyword, Pageable pageable,HttpServletRequest request) {
        StringBuilder pathParams = new StringBuilder();
        Criteria<${model_name}> criteria = new Criteria<${model_name}>();
        //其他条件自行添加
        criteria.add(Restrictions.eq("delFlag", false, true));
        
//		if (StringUtils.isNotEmpty(keyword)) {
//			criteria.add(Restrictions.or(
//                  Restrictions.like("查询字段",keyword,true),
//                  Restrictions.like("查询字段",keyword,true)));
//			pathParams.append("&keyword=").append(keyword);
//		}
        Page<${model_name}> page = ${model_name?uncap_first}Repository.findAll(criteria,pageable);
        
        if (null == page || CollectionUtils.isEmpty(page.getContent())) {
			throw new SysRuntimeException("Err_COMMON_05", HttpStatus.NO_CONTENT);
		}
        
        List<${model_name}Response> contents = new ArrayList<${model_name}Response>();
        if(page.getContent() != null && page.getContent().size() > 0){
            for(${model_name} ${model_name?uncap_first} : page.getContent()){
                ${model_name}Response ${model_name?uncap_first}Response = new ${model_name}Response();
                BeanUtils.copyPropertiesIgnoreNullValue(${model_name?uncap_first}, ${model_name?uncap_first}Response);
                contents.add(${model_name?uncap_first}Response);
            }
        }
        List<Link> list = LinkUtils.prepareLinks(pageable.getPageNumber(),
        pageable.getPageSize(), request, page, pathParams.toString());
        PagedResources<${model_name}Response> pagedResources = new PagedResources<${model_name}Response>(
            contents, new PagedResources.PageMetadata(page.getSize(), page.getNumber(),
            page.getTotalElements(), page.getTotalPages()),list);
        return new ResponseEntity<PagedResources<${model_name}Response>>(pagedResources, HttpStatus.OK);
    }

    public ResponseEntity<?> get${model_name}ById(String id) {
        ${model_name} ${model_name?uncap_first} = ${model_name?uncap_first}Repository.findOne(id);
        if(${model_name?uncap_first} == null){
            throw new SysRuntimeException("Err_COMMON_06", HttpStatus.NO_CONTENT);
        }
        
        ${model_name}Response ${model_name?uncap_first}Response = new ${model_name}Response();
        BeanUtils.copyPropertiesIgnoreNullValue(${model_name?uncap_first}, ${model_name?uncap_first}Response);
        
        return new ResponseEntity<${model_name}Response>(${model_name?uncap_first}Response,HttpStatus.OK);
    }
    
    public ResponseEntity<?> add${model_name}(Add${model_name}Request add${model_name}Request, HttpServletRequest request) {
        ${model_name} new${model_name} = new ${model_name}();
        BeanUtils.copyPropertiesIgnoreNullValue(add${model_name}Request, new${model_name});
        new${model_name}.setId(UUID.randomUUID().toString());
        
        Date currentTime = new Date();
        new${model_name}.setCreateTime(currentTime);
        new${model_name}.setUpdateTime(currentTime);
        new${model_name}.setDelFlag(false);
        
        String userName = getUserName(request);
        new${model_name}.setCreateBy(userName);
        new${model_name}.setUpdateBy(userName);
        
        ${model_name?uncap_first}Repository.save(new${model_name});
        
        CreatedResponse response = new CreatedResponse(new${model_name}.getId());
		return new ResponseEntity<CreatedResponse>(response, HttpStatus.CREATED);
    }
    
    public ResponseEntity<?> update${model_name}(String id,Update${model_name}Request update${model_name}Request, HttpServletRequest request) {
        ${model_name} ${model_name?uncap_first} = ${model_name?uncap_first}Repository.findOne(id);
        if (${model_name?uncap_first} == null) {
            throw new SysRuntimeException("Err_COMMON_06", HttpStatus.NOT_FOUND);
        }
        BeanUtils.copyPropertiesIgnoreNullValue(update${model_name}Request, ${model_name?uncap_first});
        ${model_name?uncap_first}.setUpdateTime(new Date());
        
        String userName = getUserName(request);
        ${model_name?uncap_first}.setUpdateBy(userName);
        ${model_name?uncap_first}Repository.save(${model_name?uncap_first});
        return new ResponseEntity<HttpStatus>(HttpStatus.OK);
    }
    
    public ResponseEntity<?> delete${model_name}(String id, HttpServletRequest request) {
      ${model_name} ${model_name?uncap_first} = ${model_name?uncap_first}Repository.findOne(id);
        if (${model_name?uncap_first} == null) {
            throw new SysRuntimeException("Err_COMMON_06", HttpStatus.NOT_FOUND);
        }
        
        ${model_name?uncap_first}.setUpdateTime(new Date());
        ${model_name?uncap_first}.setDelFlag(true);
        
		String userName = getUserName(request);
        ${model_name?uncap_first}.setUpdateBy(userName);
        
        ${model_name?uncap_first}Repository.save(${model_name?uncap_first});
        
        return new ResponseEntity<HttpStatus>(HttpStatus.OK);
    }

}